var f = window
f.iScroll = true
var lyricUtils = {
    lrcList: [],
    elem: null,
    album: "",
    artist: "",
    songTitle: "",
    byBody: "",
    offset: "",
    maxLineIndex: 0,
    cLine: null,
    scrollDiv: null,
    isTimeLrc: !1,
    offsetMap: {},
    lyric_txtHalfHeight: 0,
    scrollInited: 0,
    iscrollUrl: "/static/js/scroll.js",
    lrcmap: {},
    parseLyricData: function (t) {
        var i = this;
        i.lrcList = [];
        var e, a, n, s = "", o = 0;
        for (e = (t = t || "").replace(new RegExp("\\n", "g"), "_!!_").trim().split("_!!_"), i.isTimeLrc = !1, a = e.length; o < a; o++) {
            var r = (n = e[o]).lastIndexOf("]"), l = n.substring(0, r + 1), c = n.substring(r + 1),
                d = l.replace(new RegExp("\\[", "g"), "").split("]"), m = d.length - 1, p = "", u = 0;
            if (0 < m)for (i.isTimeLrc = !0; m--;)if (-1 != (p = d[m]).indexOf("al:")) u = p.indexOf(":"), i.album = p.substring(u + 1); else if (-1 != p.indexOf("ar:")) u = p.indexOf(":"), i.artist = p.substring(u + 1); else if (-1 != p.indexOf("ti:")) u = p.indexOf(":"), i.songTitle = p.substring(u + 1); else if (-1 != p.indexOf("by:")) u = p.indexOf(":"), i.byBody = p.substring(u + 1); else if (-1 != p.indexOf("offset:")) u = p.indexOf(":"), i.offset = p.substring(u + 1); else {
                p = -1 != p.indexOf(".") ? p : p + ".00", c = c.trim();
                var g = parseInt(i.playTime2ms(p) - i.offset, 10);
                p = i.ms2playTime(g), c && (i.lrcList.push({time: p, context: c}), s = p)
            } else i.lrcList.push({time: s || "00:00.00", context: c})
        }
        i.isTimeLrc && i.lrcList.sort(function (t, e) {
            return i.compare(t.time, e.time) ? -1 : 1
        }), i.maxLineIndex = i.lrcList.length
    },
    playTime2ms: function (t) {
        return 60 * parseInt(t.substring(0, t.indexOf(":")), 10) * 1e3 + 1e3 * parseInt(t.substring(t.indexOf(":") + 1, t.indexOf(".")), 10) + parseInt(t.substring(t.indexOf(".") + 1), 10)
    },
    ms2playTime: function (t) {
        var e = parseInt(t / 6e4, 10), i = parseInt(t / 1e3 % 60, 10), a = t - 6e4 * e - 1e3 * i;
        return (9 < e ? "" : "0") + e + ":" + (9 < i ? "" : "0") + i + "." + (9 < a ? "" : "0") + a
    },
    compare: function (t, e, i) {
        var a = this;
        return "<" == (i = i || "<=") ? a.playTime2ms(t) < a.playTime2ms(e) : "<=" == i ? a.playTime2ms(t) <= a.playTime2ms(e) : ">" == i ? a.playTime2ms(t) > a.playTime2ms(e) : ">=" == i ? a.playTime2ms(t) >= a.playTime2ms(e) : void 0
    },
    getLrc: function (t) {
        var a = this;
        if (a.elem && a.elem.length) {
            a.elem.empty();
            s = function (t) {
                var e;
                a.parseLyricData(M.decodeHTML(t))
                e = a.makeLrcHtml()
                e ? a.elem.html(e) : a.elem.html('<p class="lyric__para">该歌曲暂无歌词</p>')
                a.lyric_txtHalfHeight = $("#lyric_txt").height() / 2
            };
            s(t)
        }
    },
    showCurrentLrcLine: function (t) {
        var e = this;
        if (e.isTimeLrc) {
            t = parseInt(t);
            var i, a = e.lrcList.length;
            for (i = 0; i < a; i++) {
                if (t < parseInt(e.playTime2ms(e.lrcList[i].time) / 1e3))break
            }
            var n = document.getElementById("line_" + (i - 1));
            n && e.cLine != n && (e.cLine && (e.cLine.className = "lyric__para"), n.className = "lyric__para current", e.cLine = n), e.scrollLrcLine(i)
        }
    },
    currentLine: null,
    checkY: function (t) {
        if (!this.elem)return !1;
        var e = parseInt(this.elem.parent().height()) - parseInt(this.elem.height());
        return t < e && (t = e), 0 < t && (t = 0), t
    },
    scrollLrcLine: function (t) {
        var e = this;
        if (null !== e.currentLine && t == e.currentLine)return !1;
        e.currentLine = t, (t = parseInt(t)) < 0 && (t = 0);
        var i = 0;
        parseInt(e.elem[0].style.webkitTransform.split(",")[1]);
        if (e.currentLine in e.offsetMap) i = e.offsetMap[e.currentLine]; else {
            var a = 0 == e.currentLine ? e.elem.find("p").eq(0) : e.elem.find("p").eq(e.currentLine - 1);
            if (a.length) {
                var n = a.offset(), s = -(n.top - a.parent().offset().top - e.lyric_txtHalfHeight + n.height / 2);
                i = e.offsetMap[e.currentLine] = s
            }
        }
        i = e.checkY(i), M.scrollLrc && !M.scrollLrc.lock && e.elem && e.elem[0] && (e.elem[0].style.webkitTransition = "-webkit-transform 0.3s ease-out", e.elem[0].style.webkitTransform = "translate3d(0px," + i + "px,0px)")
    },
    makeLrcHtml: function () {
        var t = this.lrcList;
        if (t) {
            var e = t.length, i = 0, a = [];
            for (this.isTimeLrc || a.push('<p class="lyric__para">抱歉，该歌词暂不支持自动滚动</p><p class="lyric__para">------------------------------------------</p>'); i < e; i++) {
                var n = Math.floor(($(window).width() - 20) / 16), s = t[i].context;
                s.length > n ? a.push('<p class="lyric__para" id="line_' + i + '" style="height:auto;white-space:normal;padding:9px 0;line-height:1.5;">' + s + "</p>") : a.push('<p class="lyric__para" id="line_' + i + '">' + s + "</p>")
            }
            return a.join("")
        }
        return ""
    },
    init: function (t) {
        var a = this;
        a.elem = $(".js_lrc_list");
        a.elem.html('<p class="lyric__para" style="opacity:.5">歌词加载中...</p>');
        a.getLrc(t)
        a.initScroll()
    },
    initScroll: function () {
        var t = this;
        t.scrollInited || M.loadUrl(t.iscrollUrl, function () {
            f.iScroll && (t.scrollInited = 1, M.scrollLrc = new iScroll("lyric_txt", {hScroll: !1}), t.elem.on("touchstart", function () {
                M.scrollLrc && (M.scrollLrc.lockTimer && clearTimeout(M.scrollLrc.lockTimer), M.scrollLrc.lock = !0)
            }).on("touchend", function () {
                M.scrollLrc && (M.scrollLrc.lockTimer = setTimeout(function () {
                    M.scrollLrc.lock = !1
                }, 1e3))
            }))
        })
    }
}